﻿using System.Linq;
using System.Text;
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TheGame
{
    //Gestisce grafica ed effetti sonori di uno Slig
    public class Slig : Character
    {
        #region VARS
        public Animation START;
        public Animation PASS1;
        public Animation PASS2;
        public Animation PASS3;
        public Animation PASSsTOP;
        public Animation ONEPASS;
        public Animation RUN1;
        public Animation RUN2;
        public Animation RUN3;
        public Animation RUN4;
        public Animation RUNsTART;
        public Animation RUNsTOP1;
        public Animation HEELP;
        public Animation RUNsTOP2;
        public Animation SHOOT;
        public Animation SHOOTsTART;
        public Animation SHOOTsTOP;
        public Animation TURN;
        public Animation WAIT;
        public Animation WAKE;
        public Animation SLEEP;
        public Animation WHAAT;
        public Animation FREEZE;
        public Animation DOWNPLANE;
        public Animation SPLAT;
        public Animation POSSESSED;
        public Animation JUSTPOSSESSED;
        public Animation SUICIDE;
        public Animation OUCH;

        SoundEffect Sound_PASS;
        SoundEffect Sound_TURN;
        SoundEffect Sound_WHAAT;
        SoundEffect Sound_FREEZE;
        SoundEffect Sound_SPLAT;
        SoundEffect Sound_HELP;
        SoundEffect Sound_SHOOT;
        SoundEffect Sound_POSSESSED;

        Texture2D sLIG_BBOX_PNG;
        public Rectangle SligEye;
        public Rectangle SligEar;

        public SligAI AI;
        public string state;
        public Point position;
        public Point defaultDrawing;
        public Point defaultBounding;
        public Level levelDefault;

        public int AI_LEVEL = 3;
        public int EYE_CAPACITY = 100; 
        #endregion

        public Slig(Game1 game, string state,int AI_Level, Point position)
        {
            this.game = game;
            this.state = state;
            this.AI_LEVEL = AI_Level;
            this.position = position;

            Sound_PASS = game.SligFactory.Sound_PASS;
            Sound_TURN = game.SligFactory.Sound_TURN;
            Sound_WHAAT = game.SligFactory.Sound_WHAAT;
            Sound_FREEZE = game.SligFactory.Sound_FREEZE;
            Sound_SPLAT = game.SligFactory.Sound_SPLAT;
            Sound_HELP = game.SligFactory.Sound_HELP;
            Sound_SHOOT = game.SligFactory.Sound_SHOOT;
            Sound_POSSESSED = game.SligFactory.Sound_POSSESSED;

            START = new Animation(game.SligFactory.Start_R, game.SligFactory.Start_L, AnimateRate, 4, "START", 0, 0, -1);
            PASS1 = new Animation(game.SligFactory.Pass1_R, game.SligFactory.Pass1_L, AnimateRate, 6, "PASS1", new Sound(Sound_PASS, 6), 2, 0, 1, 0, 43);
            PASS2 = new Animation(game.SligFactory.Pass2_R, game.SligFactory.Pass2_L, AnimateRate, 6, "PASS2", 2, 0, 1, 43, 86);
            PASS3 = new Animation(game.SligFactory.Pass3_R, game.SligFactory.Pass3_L, AnimateRate, 6, "PASS3", new Sound(Sound_PASS, 4), 1, 0, -1, 86, 86);
            PASSsTOP = new Animation(game.SligFactory.PassStop_R, game.SligFactory.PassStop_L, AnimateRate, 5, "PASSsTOP", 0, 0, -1, 86, 86);
            ONEPASS = new Animation(game.SligFactory.OnePass_R, game.SligFactory.OnePass_L, AnimateRate, 8, "ONEPASS", 0, 0, -1, 0, 43);
            RUN1 = new Animation(game.SligFactory.Run1_R, game.SligFactory.Run1_L, AnimateRate, 4, "RUN1", new Sound(Sound_PASS, 4), 4, 0, 4, 0, 43);
            RUN2 = new Animation(game.SligFactory.Run2_R, game.SligFactory.Run2_L, AnimateRate, 4, "RUN2", 2, 0, -1, 43, 86);
            RUN3 = new Animation(game.SligFactory.Run3_R, game.SligFactory.Run3_L, AnimateRate, 4, "RUN3", new Sound(Sound_PASS, 4), 4, 0, 4, 86, 129);
            RUN4 = new Animation(game.SligFactory.Run4_R, game.SligFactory.Run4_L, AnimateRate, 4, "RUN4", 2, 0, -1, 129, 172);
            RUNsTART = new Animation(game.SligFactory.RunStart_R, game.SligFactory.RunStart_L, AnimateRate, 4, "RUNsTART", 0, 0, -1, 0, 0);
            RUNsTOP1 = new Animation(game.SligFactory.RunStop1_R, game.SligFactory.RunStop1_L, AnimateRate, 6, "RUNsTOP1", 0, 0, -1, 86, 129);
            HEELP = new Animation(game.SligFactory.RunStop1_R, game.SligFactory.RunStop1_L, AnimateRate, 6, "HEELP", new Sound(Sound_HELP, 4), 0, 0, -1, 86, 129);
            RUNsTOP2 = new Animation(game.SligFactory.RunStop2_R, game.SligFactory.RunStop2_L, AnimateRate, 7, "RUNsTOP2", 0, 0, -1, 129, 172);
            SHOOT = new Animation(game.SligFactory.Shoot_R, game.SligFactory.Shoot_L, AnimateRate, 4, "SHOOT", new Sound(Sound_SHOOT, 1), 0, 0, -1, 0, 0);
            SHOOTsTART = new Animation(game.SligFactory.ShootStart_R, game.SligFactory.ShootStart_L, AnimateRate, 3, "SHOOtsTART", 0, 0, -1);
            SHOOTsTOP = new Animation(game.SligFactory.ShootStop_R, game.SligFactory.ShootStop_L, AnimateRate, 6, "SHOOTsTOP", 0, 0, -10);
            WAIT = new Animation(game.SligFactory.Wait_R, game.SligFactory.Wait_L, 1000 / 7, 7, "WAIT", 0, 0, -1, 0, 0);
            JUSTPOSSESSED = new Animation(game.SligFactory.Wait_R, game.SligFactory.Wait_L, 1000 / 7, 7, "JUSTPOSSESSED",new Sound(Sound_POSSESSED, 1), 0, 0, -1, 0, 0);
            WHAAT = new Animation(game.SligFactory.Wait_R, game.SligFactory.Wait_L, AnimateRate, 7, "WHAAT", new Sound(Sound_WHAAT, 1), 0, 0, -1, 0, 0);
            FREEZE = new Animation(game.SligFactory.Wait_R, game.SligFactory.Wait_L, 1000 / 7, 7, "FREEZE", new Sound(Sound_FREEZE, 1), 0, 0, -1, 0, 0);
            TURN = new Animation(game.SligFactory.Turn_L, game.SligFactory.Turn_R, AnimateRate, 11, "TURN", new Sound(Sound_TURN, 6), 0, 0, -1, 0, 0);
            SLEEP = new Animation(game.SligFactory.Sleep_R, game.SligFactory.Sleep_L, 1000 / 8, 8, "SLEEP", 0, 0, -1, 70, 70);
            WAKE = new Animation(game.SligFactory.Wake_R, game.SligFactory.Wake_L, AnimateRate, 18, "WAKE", 0, 0, -1, 70, 70);
            DOWNPLANE = new Animation(game.SligFactory.DownPlane_R, game.SligFactory.DownPlane_L, AnimateRate, 17, "DOWNPLANE", 0, 0, -1, 0, 86,200,0);
            SPLAT = new Animation(game.SligFactory.Splat_R, game.SligFactory.Splat_R, AnimateRate, 7, "DOWNPLANE",new Sound(Sound_SPLAT, 1), 0, 0, -1, 40, 40);
            POSSESSED = new Animation(game.SligFactory.Possessed_R, game.SligFactory.Possessed_L, AnimateRate, 8, "POSSESSED", 0, 0, -1, 40, 40);
            SUICIDE = new Animation(game.SligFactory.Possessed_R, game.SligFactory.Possessed_L, AnimateRate, 8, "SUICIDE", 0, 0, -1, 40, 40);
            OUCH = new Animation(game.SligFactory.Ouch_R, game.SligFactory.Ouch_L, AnimateRate, 22, "OUCH", 0, 0, -1, 43, 43);

            AI = new SligAI(this, game, state);
            Direction = 1;
            CurrentAnimation = AI.CURRENT_STATE.AnimsTODO[AI.CURRENT_STATE.CURRENT_ANIM];
            PrevAnimation = START;
            color = Color.White;
            if(state == "SLEEP")
                DrawingRec = new Rectangle(position.X - 100, position.Y - CurrentAnimation.SpriteHeight, CurrentAnimation.SpriteWidth, CurrentAnimation.SpriteHeight);//-18+43, 295,
            if (state == "SENTINELLA" || state == "WAIT")
                DrawingRec = new Rectangle(position.X - 30, position.Y - CurrentAnimation.SpriteHeight, CurrentAnimation.SpriteWidth, CurrentAnimation.SpriteHeight);
           
            boundingBox = new Rectangle(position.X, position.Y - 110, 44, 110);// + 43*2

            defaultDrawing = new Point(DrawingRec.Location.X, DrawingRec.Location.Y);
            defaultBounding = new Point(boundingBox.Location.X, boundingBox.Location.Y);

            if(AI_LEVEL == 1)
                EYE_CAPACITY = 100;
            if (AI_LEVEL == 2)
                EYE_CAPACITY = 200;
            if (AI_LEVEL == 3)
                EYE_CAPACITY = 300;
            if (AI_LEVEL == 4)
                EYE_CAPACITY = 640;
            SligEye = new Rectangle(boundingBox.X + boundingBox.Width / 2 + (320 * (Direction - 1)), boundingBox.Y, EYE_CAPACITY, 10);
            SligEar = new Rectangle(0,0,640,480);
            sLIG_BBOX_PNG = game.SligFactory.sLIG_BBOX_PNG;          
        }

        public void UpdateEyeAndEar()
        {
            SligEye = new Rectangle(boundingBox.X + boundingBox.Width / 2 + ((EYE_CAPACITY/2) * (Direction - 1)), boundingBox.Y, EYE_CAPACITY, 10);
        }

        //Gestisce la spritesheet dell'animazione corrente.
        //Definisce quale frame della spritesheet deve essere disegnato.
        public void AnimateSLIG(Animation anim)
        {
            UpdateEyeAndEar();
            if (anim.CurrentFrame == 0)
            {
                PrevAnimation = CurrentAnimation;
                PrevAnimation.Reset();
                CurrentAnimation = anim;

                if (Direction == 1)
                    DrawingRec.X = DrawingRec.X + (PrevAnimation.xEND - CurrentAnimation.xSTART);
                else
                    DrawingRec.X = DrawingRec.X + ((PrevAnimation.SpriteLWidth - PrevAnimation.xEND) - (CurrentAnimation.SpriteLWidth - CurrentAnimation.xSTART));

                DrawingRec.Y = DrawingRec.Y - (PrevAnimation.yEND - CurrentAnimation.ySTART) + (PrevAnimation.SpriteHeight - CurrentAnimation.SpriteHeight);

                if (CurrentAnimation == TURN)
                {
                    Direction = Direction * -1;
                }

                if (Direction == 1)
                {
                    anim.SpriteWidth = anim.SpriteRWidth;
                    anim.SpriteHeight = anim.SpriteRHeight;
                    anim.Sheet = anim.SheetR;
                }
                else
                {
                    anim.SpriteWidth = anim.SpriteLWidth;
                    anim.SpriteHeight = anim.SpriteLHeight;
                    anim.Sheet = anim.SheetL;
                }
                DrawingRec.Width = anim.SpriteWidth;
                DrawingRec.Height = anim.SpriteHeight;

                this.game.spriteCurrent = anim.Sheet;
            }
            if (anim.Time > anim.AnimateRate)
            {
                if (anim.CurrentFrame == 1)
                {
                    boundingBox.X += (anim.xEND - anim.xSTART) * Direction;
                    boundingBox.Y = DrawingRec.Bottom - (anim.yEND + boundingBox.Height);
                }
                anim.Time = 0f;
                if (anim.Effect != null && anim.CurrentFrame == anim.Effect.FrameToPlay)
                    anim.Effect.Play();

                if (anim.CurrentFrame == anim.FrameCount)
                {
                    anim.LastFrameDone = true;

                }
                else
                    anim.LastFrameDone = false;

                if (anim.CurrentFrame < anim.FrameCount)
                {
                    anim.SourceRect = new Rectangle(anim.CurrentFrame * anim.SpriteWidth, 0, anim.SpriteWidth, anim.SpriteHeight);
                    this.game.sourceRect = anim.SourceRect;
                    anim.Forward();
                }
            }
        }
        #region Attribute
        public Rectangle BoundigBox
        {
            get { return boundingBox; }
            set { boundingBox = value; }
        }

        public Texture2D SLIG_BBOX_PNG
        {
            get { return sLIG_BBOX_PNG; }
        } 
        #endregion
    }
}
